package com.atguigu.spring.tx.component.serivice.impl;

import com.atguigu.spring.tx.component.dao.api.EmpDao;
import com.atguigu.spring.tx.component.serivice.api.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;

/**
 * @author : zzf
 * @date : 2021/1/5
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpDao empDao;

    @Override
    public void updateTwice() {
        Integer empId = 2;
        String empName = "zz";
        empDao.updateEmpName(empId,empName);

        empId = 3;
        empName = "ff";

        // 4.执行第二次修改
        empDao.updateEmpName(empId, empName);
    }

    @Override
    public void updateSingle() throws FileNotFoundException, InterruptedException {

    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public String getEmpNameById(Integer empId) {
        return empDao.selectEmpNameById(empId);
    }

    @Override
    @Transactional(readOnly = false,propagation = Propagation.REQUIRES_NEW,isolation = Isolation.READ_COMMITTED)
    public void updateEmpName(Integer empId, String empName) {
        empDao.updateEmpName(empId, empName);
    }
}
